package cn.itcast.web.controller.system;

import cn.itcast.common.utils.Encrypt;
import cn.itcast.common.utils.MailUtil;
import cn.itcast.domain.system.Dept;
import cn.itcast.domain.system.Role;
import cn.itcast.domain.system.User;
import cn.itcast.service.system.DeptService;
import cn.itcast.service.system.RoleService;
import cn.itcast.service.system.UserService;
import cn.itcast.web.controller.BaseController;
import cn.itcast.web.mq.MailProducer;
import com.github.pagehelper.PageInfo;
import javafx.scene.input.InputMethodTextRun;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/system/user")
public class UserController extends BaseController {

	@Autowired
	private UserService userService;

	@Autowired
	private DeptService deptService;

	@Autowired
	private RoleService roleService;

	/**
	 * 分页查询
	 */
	@RequestMapping(value = "/list",name = "分页查询用户列表")
	public String list(@RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "10") int size) {
		PageInfo info = userService.findAll(page, size, getLoginCompanyId());
		request.setAttribute("page",info);
		return "system/user/user-list";
	}

	/**
	 * 进入保存页面
	 */
	@RequestMapping(value = "/toAdd",name = "进入保存用户页面")
	public String toAdd() {
		//查询所有部门
		List deptList = deptService.findAll(getLoginCompanyId());
		request.setAttribute("deptList",deptList);
		return "system/user/user-add";
	}

	/**
	 * 进入修改页面
	 */
	@RequestMapping("/toUpdate")
	public String toUpdate(String id) {
		//根据id查询用户
		User user = userService.findById(id);
		request.setAttribute("user",user);
		//查询所有的部门
		List deptList = deptService.findAll(getLoginCompanyId());
		request.setAttribute("deptList",deptList);
		return "system/user/user-update";
	}

	@Autowired
	private MailProducer mailProducer;

	/**
	 * 保存或者更新
	 */
	@RequestMapping("/edit")
	public String edit(User user) throws Exception {
		//1.设置企业参数
		user.setCompanyId(getLoginCompanyId());
		user.setCompanyName(getLoginCompanyName());
		if (StringUtils.isEmpty(user.getId())) {
			//保存
			String to=user.getEmail();
			String subject="欢迎使用saas-export系统";
			String content="尊敬的用户："+user.getUserName()+"您好。请访问http://localhost:8088/登陆系统，访问账号："+user.getEmail()
					+",访问密码："+user.getPassword();

			//需要对密码进行加密
			String password = Encrypt.md5(user.getPassword(),user.getEmail());//密码，盐
			user.setPassword(password);
			userService.save(user);

			//MailUtil.sendMsg(to,subject,content);
			Map map = new HashMap<>();
			map.put("to",to);
			map.put("subject",subject);
			map.put("content",content);
			mailProducer.send(map);
		}else{
			//更新
			userService.update(user);
		}
		return "redirect:/system/user/list.do";
	}

	/**
	 * 根据id删除
	 */
	@RequestMapping(value = "/delete",name = "根据id删除用户")
	public String delete(String id) {
		userService.delete(id);
		return "redirect:/system/user/list.do";
	}


	/**
	 * 进入到角色分配的页面
	 *     参数：用户id
	 *     业务：
	 *      1.根据id查询用户
	 *      2.查询所有的角色列表
	 *      3.跳转到页面
	 */
	@RequestMapping("/roleList")
	public String roleList(String id) {
		//1.根据id查询用户
		User user = userService.findById(id);
		request.setAttribute("user",user);
		//2.查询所有的角色列表
		List roleList = roleService.findAll(getLoginCompanyId());
		request.setAttribute("roleList",roleList);
		//3.查询当前用户具有的所有角色
		List<Role>  userRoles =  roleService.findByUserId(id);
		//4.构造用户角色的id字符串
		String userRoleStr = "";
		for (Role userRole : userRoles) {
			userRoleStr+=userRole.getId();
		}
		request.setAttribute("userRoleStr",userRoleStr);
		return "system/user/user-role";
	}

	/**
	 * 实现角色分配
	 *     参数：
	 *      1.用户id
	 *      2.所有勾选的角色id
	 *          * 页面传递到controller中是一组同名的参数
	 *          i。可以通过字符串接收：多个参数之间通过“，”隔开
	 *          ii。可以通过字符串数组接收
	 *
	 */
	@RequestMapping("/changeRole")
	public String changeRole(String userid,String [] roleIds) {
		//业务
		roleService.changeRole(userid,roleIds);
		return "redirect:/system/user/list.do";
	}
}
